﻿using System.Net;

namespace MeiWinForms.comm;

public static class Utils
{
    public static string GetLocalIp()
    {
        try
        {
            // 获取当前主机的主机名
            string hostName = Dns.GetHostName();

            // 获取主机的IP地址信息
            IPHostEntry hostEntry = Dns.GetHostEntry(hostName);

            // 遍历所有IP地址
            foreach (IPAddress? ip in hostEntry.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($@"发生错误: {ex.Message}");
        }

        return "127.0.0.1";
    }
}